<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/16
 * Time: 10:39
 */

namespace app\controller;
use app\domain\car;
use app\domain\highPecData;
use app\domain\peccancyInfo;
use app\domain\user;
use rap\web\Request;
use rap\config\Config;
use app\server\request as requestServer;
use app\server\setScore;

class RuleController
{
    /**
     * 获取用户违章信息
     * @param Request $request
     */
    public function getUserPeccancyInfo (Request $request) {
        //判断请求方法
        if ($request->method() == "POST") {
            $token = $request->param("token", '');
            $car_code = $request->param('car_code', '');
            $engine_code = $request->param('engine_code', '');
            $class_code = $request->param('class_code', '');

            $car_co = substr($car_code, 0, 4);
            if ($car_co != "鲁M"){
                return CommonController::false(404,"暂时只支持滨州地区用户", []);
            }

            $config = Config::getFileConfig();

            $params = array(
                'key' => $config['app_info']['wz_key'],
                'city'  => "SD_BZ",
                'hphm' => $car_code,
                'hpzl' => '02',
                'engineno'=> substr($engine_code, 1, 6),
                'classno'   => substr($class_code,-6)
            );

            $data = JvheController::juhecurl($config['app_info']['wzUrl'], $params, 1);

            if ($data['resultcode'] = "208"){
                return [
                    'code' => 404,
                    'success' => false,
                    'msg' => '您所查询的城市正在维护或未开通查询',
                    'data' => [],
                ];
            }

            $data = (array)json_decode($data)->result;


            $list = $data['lists'];

            if ($list) {
                $res = array();
                for ($i = 0; $i < count($list); $i++) {
                    $res[$i] = (array)$list[$i];
                }
                $res1 = peccancyInfo::addPecInfo($res, $_SESSION[$token]['user_id'], $car_code);
                if ($list) {
                    $info = setScore::getScoreInfo(['isdelete' => 0, 'type_name' => '违章查询'], "score");
                    $where = [
                        'user_id' => $_SESSION[$token]['user_id'],
                        'gold' => $info['score'],
                        'remark' => "违章查询",
                        'secret' => Config::getFileConfig()['app_info']['app_secret'],
                    ];
                    CommonController::modifyGold($where);
                    return [
                        'code' => 200,
                        'success' => true,
                        'msg' => '获取信息成功',
                        'data' => $list,
                    ];
                } else {
                    return [
                        'code' => 404,
                        'success' => false,
                        'msg' => '暂无违章信息',
                        'data' => $list,
                    ];
                }
            } else {
                return [
                    'code' => 404,
                    'success' => false,
                    'msg' => '暂无违章信息',
                    'data' => [],
                ];
            }



        }
    }

    /**
     * 违章高发地
     * @param Request $request
     * @return array|bool|mixed|string
     */
    public function getHighPecPlace(Request $request) {

        if ($request->method() == "POST") {
            //经纬度信息
            $lat = $request->param('lat', '');//纬度37.371418
            $lon = $request->param('lon', '');//纬度118.026748
            $limit = $request->param('limit', 50);//条数
            $token = $request->param('token', 0);//用户令牌

            if (!$lat) return CommonController::false(401, "参数不能为空", []);
            if (!$lon) return CommonController::false(401, "参数不能为空", []);

            $info = setScore::getScoreInfo(['isdelete' => 0, 'type_name' => '附近违章'], "score");

            if (!$_SESSION[$token]['user_id']) {
                $res = CommonController::getUserInfo($token);
                $data = CommonController::getPersonalInfo($res, $token);

            }
            $param = [
                'lat' => $lat,
                'lon' => $lon,
                'page' => 1,
                'pagesize' => $limit,
                'r' => 2000,
                'key' => "28473af2fb9dc6f23bae7fddd602e97c"
            ];
            $paramString = http_build_query($param);
            $data = CommonController::getHighPecPlace($paramString);

            highPecData::addHighPecLog($_SESSION[$token]['user_id'], $data['result']['list']);
            if ($data['result']['list']) {

                if ($_SESSION[$token]['gold'] < $info['score']) {
                    return CommonController::false(404, '壁虎币余额不足，请及时充值！', []);
                } else {
                    $where = [
                        'user_id' => $_SESSION[$token]['user_id'],
                        'gold' => $info['score'],
                        'remark' => "附近违章",
                        'secret' => Config::getFileConfig()['app_info']['app_secret'],
                    ];
                    CommonController::modifyGold($where);
                }
                return CommonController::success("获取数据成功,扣除0个壁虎币",$data['result']['list']);
            } else {
                return CommonController::false(404, '暂无查询信息', []);
            }
        }
    }

    /**
     * 查询驾照分数
     * @param Request $request
     * @return array
     */
    public function  getUserDriverLicenseScore(Request $request)
    {

        if ($request->method() == "POST") {
            $token = $request->param("token", 0);
            $driveNum = $request->param("driveNum", '370724199502207778');//372301199704220319
            $fileNum = $request->param("fileNum", '370730066647');//371601445123

            $where = [
                'jszh' => $driveNum,
                'dabh' => $fileNum,
                'key' => '86070e73e280e1a9ffbb82bc6b0df281',
                'sf' => "山东"
            ];
            $url = "http://v.juhe.cn/driverLicense/query.php?";

            if (!$_SESSION[$token]['user_id']) {
                $res = CommonController::getUserInfo($token);
                $data = CommonController::getPersonalInfo($res, $token);

            }
                $data = requestServer::getRequestMethod($url, $where);
                $info = setScore::getScoreInfo(['isdelete' => 0, 'type_name' => '驾照查分'], "score");


                if ($data['reason'] == "success") {
                    $data['score'] = 12 - $data['result'];
                    $data = [
                        'class' => "C1",
                        'score' => $data['score'],
                        'status' => "正常",
                    ];
                    if ($_SESSION[$token]['gold'] < $info['score']) {
                        return CommonController::false(404, '壁虎币余额不足，请及时充值！', []);
                    } else {
                        $where = [
                            'user_id' => $_SESSION[$token]['user_id'],
                            'gold' => $info['score'],
                            'remark' => "驾照查分数",
                            'secret' => Config::getFileConfig()['app_info']['app_secret'],
                        ];
                        CommonController::modifyGold($where);
                    }
                    return CommonController::success('获取信息成功', $data);
                } else {
                    return CommonController::false(404, '暂无查询信息');
                }
        }
    }
}